//
//  LFViewController.m
//  LFPhoneInfo
//
//  Created by James on 06/02/2019.
//  Copyright (c) 2019 James. All rights reserved.
//

#import "LFViewController.h"
#import "LFPhoneInfo.h"


@interface LFViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *deviceInfoList;

@property (nonatomic, strong) NSMutableArray <NSString *>*dataArray;

@end

@implementation LFViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"设备检查";
    [self setLeftItemWithIcon:[UIImage imageNamed:@"back"] title:nil selector:@selector(onClickBack)];

    [SVProgressHUD showWithStatus:@"检查中……"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        NSString *result = @"完成，您的设备是正版！";
        if (LFPhoneInfo.deviceIsJailbreak) {
            result = @"您设备可能被越狱";
        }
        
        if (LFPhoneInfo.deviceIsSimulator) {
            result = @"您设备可能是模拟器";
        }
        
        [SVProgressHUD showSuccessWithStatus:result];
        [self createLabels];
        [self showPhoneInfo];
    });
}

- (void)onClickBack {
    [self.navigationController popViewControllerAnimated:YES];
}

// 展示手机信息
-(void)showPhoneInfo{
    // 获取当前设备的具体型号字符串 e.g. @"iPhone X" @"iPhone XS" @"iPhone XS Max
    [self.dataArray addObject:[NSString stringWithFormat:@"设备型号：%@",LFPhoneInfo.deviceTypeString]];
    // 手机系统名称 e.g. @"iOS"
    [self.dataArray addObject:[NSString stringWithFormat:@"设备系统名称：%@",LFPhoneInfo.deviceSystemName]];
    // 手机系统版本 e.g. @"9.3.5"
    [self.dataArray addObject:[NSString stringWithFormat:@"设备系统版本：%@",LFPhoneInfo.deviceSystemVersion]];
    // 当前设备电池电量百分比，取值范围 0 至 1.0，如果返回 -1.0 表示无法识别电池
    NSString *batteryLevel = [NSString stringWithFormat:@"当前电量：%d %%",(int)(LFPhoneInfo.deviceBatteryLevel * 100)];
    if (LFPhoneInfo.deviceBatteryLevel == -1) {
        batteryLevel = @"当前电量：无法识别电池";
    }
    [self.dataArray addObject:batteryLevel];
    // 屏幕逻辑尺寸 e.g. 逻辑像素尺寸为 2208x1242（屏幕实际物理像素尺寸是 1920x1080）
    [self.dataArray addObject:[NSString stringWithFormat:@"屏幕逻辑尺寸：宽：%@，高：%@",@(LFPhoneInfo.deviceLogicalScreenSize.width), @(LFPhoneInfo.deviceLogicalScreenSize.height)]];
    // 当前设备总内存, 返回值为兆 MB, e.g. iPhone 总内存为 2048 MB
    [self.dataArray addObject:[NSString stringWithFormat:@"设备总内存：%.1f GB",LFPhoneInfo.deviceTotalMemory/1024]];
    // 当前 App 占用的设备内存，返回值为兆 MB, e.g. 占用 43 MB
    [self.dataArray addObject:[NSString stringWithFormat:@"App占用内存：%.1f MB",LFPhoneInfo.appTakeUpMemory]];
    // 当前磁盘总空间，返回值为兆 MB，0为异常 e.g. 总共 16 GB 即 16384 MB
    [self.dataArray addObject:[NSString stringWithFormat:@"设备总存储：%.1f GB",LFPhoneInfo.deviceTotalDisk/1024]];
    // 当前磁盘未使用，返回值为兆 MB，0为异常 e.g. 空闲 2200 MB
    [self.dataArray addObject:[NSString stringWithFormat:@"设备剩余存储：%.1f GB",LFPhoneInfo.deviceFreeDisk/1024]];
    // 当前磁盘已经使用，返回值为兆 MB，0为异常 e.g. 已使用 2200 MB
    [self.dataArray addObject:[NSString stringWithFormat:@"设备已用存储：%.1f GB",LFPhoneInfo.deviceUsedDisk/1024]];
    // 当前设备的 CPU 数量
    [self.dataArray addObject:[NSString stringWithFormat:@"CPU 个数：%ld",(long)LFPhoneInfo.deviceCPUNum]];
    // 当前 SIM 个数
    [self.dataArray addObject:[NSString stringWithFormat:@"SIM 个数：%ld",(long)LFPhoneInfo.deviceSIMCount]];
    // 当前设备网络状态 e.g. @"WiFi" @"无服务" @"2G" @"3G" @"4G" @"LTE" @"WWAN"
    [self.dataArray addObject:[NSString stringWithFormat:@"网络状态：%@",LFPhoneInfo.deviceNetType]];
    // 当前设备局域网 ip 地址
    [self.dataArray addObject:[NSString stringWithFormat:@"局域网 IP 地址：%@",LFPhoneInfo.deviceLANIp]];
    // 当前设备是否使用网络代理, YES 是使用，NO 为未使用
    [self.dataArray addObject:[NSString stringWithFormat:@"是否使用代理：%@",LFPhoneInfo.deviceIsUseProxy?@"是":@"否"]];
    // 设备类型名称 e.g. @"iPhone", @"iPod touch", @"iPad"
    [self.dataArray addObject:[NSString stringWithFormat:@"设备类型：%@",LFPhoneInfo.deviceModel]];
    // 当前设备用户设置的名称，设置->通用->关于本机->名称 e.g. "My iPhone"
    [self.dataArray addObject:[NSString stringWithFormat:@"设备用户名：%@",LFPhoneInfo.deviceSettingName]];
//    // 判断当前设备是不是iPhone，YES 是 iPhone 设备，NO不是
//    [self.dataArray addObject:[NSString stringWithFormat:@"是否是iPhone真机：%@",LFPhoneInfo.deviceIsIPhone?@"是":@"否"]];
//    // 判断当前设备是不是 iPhoneX 系列，YES 是 iPhoneX 系列，NO 不是
//    [self.dataArray addObject:[NSString stringWithFormat:@"是否是刘海屏系列：%@",LFPhoneInfo.deviceIsIPhoneX?@"是":@"否"]];
//    // 判断当前设备是不是iPad，YES 是 iPad 设备，NO不是
//    [self.dataArray addObject:[NSString stringWithFormat:@"是否是iPad：%@",LFPhoneInfo.deviceIsIPad?@"是":@"否"]];
//    // 判断当前设备是不是iPod，YES 是 iPod 设备，NO不是
//    [self.dataArray addObject:[NSString stringWithFormat:@"是否是iPod：%@",LFPhoneInfo.deviceIsIPod?@"是":@"否"]];
//    // 判断当前设备是不是模拟器，YES 是 模拟器，NO不是
//    [self.dataArray addObject:[NSString stringWithFormat:@"是否是模拟器：%@",LFPhoneInfo.deviceIsSimulator?@"是":@"否"]];
    // 当前设备电池电量百分比，取值范围 0 至 1.0，如果返回 -1.0 表示无法识别电池
    
    // 通过系统框架获取设备运营商 e.g. @"中国移动" @"中国联通" @"中国电信"
    NSString *carrieName = LFPhoneInfo.deviceCarrierList.firstObject;
    if (LFPhoneInfo.deviceSIMCount > 1) {
        carrieName = [NSString stringWithFormat:@"%@,%@",LFPhoneInfo.deviceCarrierList.firstObject, LFPhoneInfo.deviceCarrierList.lastObject];
    }
    [self.dataArray addObject:[NSString stringWithFormat:@"网络运营商：%@",carrieName]];
    // 当前 APP 最近的一次更新时间(或安装时间) e.g. @"2019-06-01 12:32:38 +0000"
    [self.dataArray addObject:[NSString stringWithFormat:@"App 更新时间：%@",LFPhoneInfo.appUpdateDate]];
    // 当前设备是否越狱,模拟器会认为已经越狱
//    [self.dataArray addObject:[NSString stringWithFormat:@"是否越狱：%@",LFPhoneInfo.deviceIsJailbreak?@"是":@"否"]];

    [self.deviceInfoList reloadData];
}

//创建label
-(void)createLabels{
    self.view.backgroundColor = UIColorFromHex(0xf6f6f6);
    
    UIImageView *mainImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Apple1024"]];
    mainImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:mainImage];
    [mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(25));
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.height.equalTo(@(100));
    }];
    
    
    [self.view addSubview:self.deviceInfoList];
    [_deviceInfoList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainImage.mas_bottom).offset(25);
        make.left.right.bottom.equalTo(@(0));
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = UIColorFromHex(0xf6f6f6);
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = MainThemeColor;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (UITableView *)deviceInfoList{
    if(!_deviceInfoList){
        _deviceInfoList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _deviceInfoList.dataSource = self;
        _deviceInfoList.delegate = self;
        _deviceInfoList.backgroundColor = UIColorFromHex(0xf6f6f6);
        [_deviceInfoList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    }
    return _deviceInfoList;
}

@end
